<?php

/**
 * @author hungnq@younetco.com
 */
class Core_Model_DbTable_Phases extends YouNet_Db_Table
{

	const TABLE_NAME = 'contest_phases';
	protected $_name = self::TABLE_NAME;
	protected $_rowClass = 'Core_Model_Phase';

	const IS_ALLOW_COMMENT	= 'is_allow_comment';
	const IS_ALLOW_SUBMIT	= 'is_allow_submit';
	const IS_ALLOW_VIEW		= 'is_allow_view';
	const IS_ALLOW_VOTE		= 'is_allow_vote';
	
	protected $_referenceMap = array(
		'Contest' => array(
			'columns' => 'id_contest',
			'refTableClass' => 'Core_Model_DbTable_Contests',
			'refColumns' => 'id'
		),
	);
	private static $_instance;
	/**
	 * @author hungnq
	 * @return Core_Model_DbTable_Phases
	 */
	public static function getInstance()
	{
		if(is_null(self::$_instance))
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function listPhasesByContest($id_contest)
	{
		$query = $this->select()
				->where('id_contest=?',$id_contest);
		return $this->fetchAll($query);
	}
	/**
	 *
	 * @param type $field_name
	 * @param type $id_contest 
	 */
	private function _isAllowed($field_name, Core_Model_Contest $contest)
	{
		$query = $this->select()
				->from($this->_name, array('count(id) as is_allowed'))
				->where("$field_name=1")
				->where('? >= date_start', $contest->getCurrentTimeWithTZ())
				->where('? <= date_end', $contest->getCurrentTimeWithTZ())
				->where('id_contest=?', $contest->id);

		$result = $this->fetchAll($query)->current();

		return $result->is_allowed;
	}

	public function isAllowedSubmit(Core_Model_Contest $contest)
	{
		return $this->_isAllowed(self::IS_ALLOW_SUBMIT, $contest);
	}

	public function isAllowedVote(Core_Model_Contest $contest)
	{
        if( empty($contest->id) )
            return false;
		$key = Core_Cache_IKeys::MEMCACHE_CONTEST_IS_ALLOW_VOTE.$contest->id;
		$value = YouNet_Util::getMemCache()->load($key);
		if( $value === false )
		{
			$value = $this->_isAllowed(self::IS_ALLOW_VOTE, $contest);
			$duration	= mktime(23, 59, 59) - $contest->getCurrentTimeWithTZ();
			if( $duration > 0)
			{
				YouNet_Util::getMemCache()->save(
					$value?1:0,
					$key,
					array(),
					$duration
				);
			}
			elseif( $duration < 0)
			{
				YouNet_Util::getMemCache()->save(
					$value?1:0,
					$key,
					array(),
					$duration + 24*3600
				);
			}
		}
		return $value;
	}

	public function isAllowedComment(Core_Model_Contest $contest)
	{
		return $this->_isAllowed(self::IS_ALLOW_COMMENT, $contest);
	}
	
	public function isAllowedView(Core_Model_Contest $contest)
	{
		return $this->_isAllowed(self::IS_ALLOW_VIEW, $contest);
	}
	
	/**
	 *
	 * @param type $id_contest
	 * @return array(Core_Model_DbTable_Contests::SPAN_TIME_DATE_START => ?,Core_Model_DbTable_Contests::SPAN_TIME_DATE_END => ?)
	 */
	public function getTimeSpan($id_contest)
	{
		$query = $this->select()
				->from($this->_name, array(
					Core_Model_DbTable_Contests::SPAN_TIME_DATE_START	=> new Zend_Db_Expr('MIN(date_start)'),
					Core_Model_DbTable_Contests::SPAN_TIME_DATE_END		=> new Zend_Db_Expr('MAX(date_end)')
				))				
				->where('id_contest=?', $id_contest)
				->group('id_contest');

		$result = $this->fetchAll($query)->current();
		return $result;
	}
}